home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / TECHNICA / AUTOCAD / H108.ZIP / RANDOM.ZIP / LRAND.C next >
C/C++ Source or Header  |  1991-08-28  |  3KB  |  127 lines

  1. /*    LRAND.C
  2.   This ADS program is makes a random number generator available
  3.   to LISP.
  4.  
  5.   Program developed by Clayton L. Cranor
  6.                        Denali Computing Services
  7.                        3520 International Way
  8.                        Fairbanks, Alaska
  9.                        (907)451-5003
  10.                        CI$ 71121,773
  11.  
  12.  
  13.   Copyright (C) 1991 by Denali Computing Services.
  14.  
  15. Permission is hereby granted to anyone to do anything with this code.
  16. The template for ADS communication was modified from that supplied by
  17. Autodesk, Inc.  SOOoooo..... :
  18.      ________________________________________________________________________
  19.  
  20.       Copyright (C) 1990 by Autodesk, Inc.
  21.      
  22.       Permission to use, copy, modify, and distribute this software and its
  23.       documentation for any purpose and without fee is hereby granted.    
  24.    
  25.       THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY. 
  26.       ALL IMPLIED WARRANTIES OF FITNESS FOR ANY PARTICULAR PURPOSE AND OF 
  27.       MERCHANTABILITY ARE HEREBY DISCLAIMED.                    
  28.      ________________________________________________________________________
  29.  
  30.       Prototype for ADS application.
  31.    
  32.       by Amy Berger      
  33.       April 16, 1990
  34.   
  35.       Updated July 30, 1990
  36.  
  37. */
  38.  
  39. #include  <string.h>
  40. #include  <stdio.h>
  41. #include  <stdlib.h>
  42. #include  "c:\acad\ads\adslib.h"
  43.  
  44. static int loadfuncs();
  45.  
  46.  
  47. /* MAIN - the main routine */
  48.  
  49.  
  50. void main(argc, argv)
  51.   int argc;
  52.   char *argv[];
  53. {
  54.     int stat;
  55.     short scode = RSRSLT;          /* This is the default result code */
  56.  
  57.     ads_init(argc, argv);          /* Initialize the interface */
  58.  
  59.     for ( ;; ) {              /* Note loop conditions */
  60.  
  61.     if ((stat = ads_link(scode)) < 0) {
  62.  
  63.       printf("LRAND: bad status from ads_link() = %d\n", stat);
  64.         fflush(stdout);
  65.         exit(1);
  66.     }
  67.  
  68.     scode = RSRSLT;           /* Default return value */
  69.  
  70.     /* Check for the following cases here */
  71.     switch (stat) {
  72.  
  73.   case RQXLOAD:         /* Register ADS external functions.
  74.                            Required for all applications.  */
  75.  
  76.         scode = loadfuncs() ? RSRSLT : RSERR;
  77.         break;
  78.  
  79.     case RQSUBR:              /* This case is normally expanded to 
  80.                            select one of the application's
  81.                            external functions, in this case,
  82.                            run the code... */
  83.  
  84.         switch (ads_getfuncode()) {
  85.             case 0:     /* run the command RAND */
  86.                   static int seed;
  87.                   int i=0;
  88.                   double x,numb=2147483648.0;     /*  this number is 2^31,
  89.                                                       the cyclic period of
  90.                                                       High C's rand()... */
  91.                   seed=rand();
  92.                   srand(seed);
  93.                   x=seed/numb;
  94.                   stat = ads_retreal(x);
  95.                   if (stat < 0)
  96.                     ads_printf("\nAbnormal return from LRAND --");
  97.                   break;
  98.  
  99.             default:
  100.                   break;
  101.         }
  102.         break;
  103.  
  104.     default:
  105.         break;
  106.     }
  107.  }
  108. }
  109.  
  110.  
  111. /* LOADFUNCS  --  Define external functions with AutoLISP.
  112.  
  113. */
  114.  
  115. static int loadfuncs()
  116. {
  117.     int status,fcode;
  118.  
  119.     fcode=0;
  120.     if ((status=ads_defun("rand",fcode)) != RTNORM) {
  121.         ads_fail("Cannot define function RAND().");
  122.         return 0;
  123.     }
  124.     return 1;
  125. }
  126.  
  127.